/*
 ** main.c for  in /home/massro_a//zappy-2011/map
 **
 ** Made by ancet massrouf
 ** Login   <massro_a@epitech.net>
 **
 ** Started on  Wed Jun 22 20:32:18 2011 ancet massrouf
** Last update Wed Jul  6 20:05:16 2011 el-hadji-malick-ndour dieng
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "map.h"
#include "usuals.h"

int get_case_position(char **tab, int x, int y, int max_x, int max_y)
{
  int k;
  if (x > max_x || y > max_y)
    {
      printf("error x or y value in get_case_position. x"
	     "and y must be lower than x_max and y_max map size");
      return (EXIT_FAILURE);
    }
  else
    {
      if (y > 0)
        {
	  k = y * max_x;
	  k = k + x + 1;
        }
      else
	k = x + 1;
    }
  return (k);
}

char **make_map(int nb_cases)
{
  int x = 0;
  int y = 0;
  char **tab;

  tab = malloc((nb_cases) * sizeof (char **));
  while (x < nb_cases)
    {
      tab[x] = malloc(8 * sizeof (char *));
      while (y < 8)
        {
	  tab[x][y] = '0';
	  y++;
        }
      y = 0;
      x++;
    }
  tab[x] = NULL;
  return (tab);
}

int random_resources_generator(char **tab, int richesse)
{
  int count_i = 0;
  int count_j = 0;
  int nb_generate = 0;

  while (tab[count_i] != NULL)
    {
      while (count_j < 8)
	{
	  if (rand() % richesse == 1 && count_j != 0)
	    {
	      tab[count_i][count_j] = '1'; /*ou un random sur un nombre plus important a partir du code ascii*/
	      nb_generate++;
	    }
	  count_j++;
	}
      count_j = 0;
      count_i++;
    }
  return (nb_generate);
}


int main(int ac, char **av)
{
  int nb_cases;
  int x_size;
  int y_size;
  static char **tab;

  if (ac != 3)
    {
      printf("%s\n", "pour utiliser la map passez lui une taille : ./a.out 50");
      return (EXIT_FAILURE);
    }
  x_size = atoi(av[1]);
  y_size = atoi(av[2]);
  if (x_size > 999 || y_size > 999 || x_size <= 0 || y_size <= 0)
    {
      printf("usage: ./a.out 0>x>999 0>y>999\n");
      return (EXIT_FAILURE);
    }
  else
    {
      nb_cases = ((x_size) * (y_size));
      tab = make_map(nb_cases);
   
      printf("vierge tab:\n");
      aff_tab(tab);

      printf("trop pauvre tab:\n");
      printf("%s %d\n\n", "nombre de ressources generates:\n", random_resources_generator(tab, TROP_PAUVRE));
      aff_tab(tab);

      printf("tres pauvre tab:\n");
      printf("%s %d\n\n", "nombre de ressources generates:\n", random_resources_generator(tab, TRES_PAUVRE));
      aff_tab(tab);

      printf("moyen tab:\n");
      printf("%s %d\n\n", "nombre de ressources generates:\n", random_resources_generator(tab, MOYEN));
      aff_tab(tab);

      printf("riche tab:\n");
      printf("%s %d\n\n", "nombre de ressources generates:\n", random_resources_generator(tab, RICHE));
      aff_tab(tab);

      printf("%s %d\n\n", "nombre de ressources generates:\n", random_resources_generator(tab, TRES_RICHE));
      aff_tab(tab);

      printf("trop riche tab:\n");
      printf("%s %d\n", "nombre de ressources generates:\n", random_resources_generator(tab, TROP_RICHE));
      aff_tab(tab);


      printf("%s", " position de la case 4, 4:\n");
      printf("%d\n\n", get_case_position(tab, 4, 4, x_size, y_size));
      printf("%s", " position de la case 4 , 3:\n");
      printf("%d\n\n", get_case_position(tab, 4, 3, x_size, y_size));
      printf("%s", " position de la case 4, 2:\n");
      printf("%d\n\n", get_case_position(tab, 4, 2, x_size, y_size));
      printf("%s", " position de la case 4 , 1:\n");
      printf("%d\n\n", get_case_position(tab, 4, 1, x_size, y_size));
      printf("%s", " position de la case 4 , 0:\n");
      printf("%d\n\n", get_case_position(tab, 4, 0, x_size, y_size));
      printf("%s", " position de la case 3 , 5:\n");
      printf("%d\n\n", get_case_position(tab, 3, 5, x_size, y_size));
      printf("%s", " position de la case 3, 4:\n");
      printf("%d\n\n", get_case_position(tab, 3, 4, x_size, y_size));
      printf("%s", " position de la case 3 , 3:\n");
      printf("%d\n\n", get_case_position(tab, 3, 3, x_size, y_size));
      printf("%s", " position de la case 3, 2:\n");
      printf("%d\n\n", get_case_position(tab, 3, 2, x_size, y_size));
      printf("%s", " position de la case 3, 1:\n");
      printf("%d\n\n", get_case_position(tab, 3, 1, x_size, y_size));
      printf("%s", " position de la case 3, 0:\n");
      printf("%d\n\n", get_case_position(tab, 3, 0, x_size, y_size));
      printf("%s", " position de la case 2,5:\n");
      printf("%d\n\n", get_case_position(tab, 2, 5, x_size, y_size));
      printf("%s", " position de la case 2, 4:\n");
      printf("%d\n\n", get_case_position(tab, 2, 4, x_size, y_size));
      printf("%s", " position de la case 2, 3:\n");
      printf("%d\n\n", get_case_position(tab, 2, 3, x_size, y_size));
      printf("%s", " position de la case 2, 2:\n");
      printf("%d\n\n", get_case_position(tab, 2, 2, x_size, y_size));
      printf("%s", " position de la case 2 , 1:\n");
      printf("%d\n\n", get_case_position(tab, 2, 1, x_size, y_size));
      printf("%s", " position de la case 2, 0:\n");
      printf("%d\n\n", get_case_position(tab, 2, 0, x_size, y_size));
      printf("%s", " position de la case 1, 5:\n");
      printf("%d\n\n", get_case_position(tab, 1, 5, x_size, y_size));
      printf("%s", " position de la case 1, 4:\n");
      printf("%d\n\n", get_case_position(tab, 1, 4, x_size, y_size));
      printf("%s", " position de la case 1 , 3:\n");
      printf("%d\n\n", get_case_position(tab, 1, 3, x_size, y_size));
      printf("%s", " position de la case 1, 2:\n");
      printf("%d\n\n", get_case_position(tab, 1, 2, x_size, y_size));
      printf("%s", " position de la case 1, 1:\n");
      printf("%d\n\n", get_case_position(tab, 1, 1, x_size, y_size));
      printf("%s", " position de la case  1, 0:\n");
      printf("%d\n\n", get_case_position(tab, 1, 0, x_size, y_size));
      printf("%s", " position de la case 0, 5:\n");
      printf("%d\n\n", get_case_position(tab, 0, 5, x_size, y_size));
      printf("%s", " position de la case 0, 4:\n");
      printf("%d\n\n", get_case_position(tab, 0, 4, x_size, y_size));
      printf("%s", " position de la case 0, 3:\n");
      printf("%d\n\n", get_case_position(tab, 0, 3, x_size, y_size));
      printf("%s", " position de la case 0 , 2:\n");
      printf("%d\n\n", get_case_position(tab, 0, 2, x_size, y_size));
      printf("%s", " position de la case 0, 1:\n");
      printf("%d\n\n", get_case_position(tab, 0, 1, x_size, y_size));
      printf("%s", " position de la case 0, 0:\n");
      printf("%d\n\n", get_case_position(tab, 0, 0, x_size, y_size));
    }
  return (EXIT_SUCCESS);
}
